package com.jxl.springboot.game.interceptor;

import com.jxl.springboot.game.utils.CookieUtils;
import com.jxl.springboot.game.utils.ParamUtils;
import com.jxl.springboot.game.utils.RedisUtils;
import com.jxl.springboot.game.web.constant.Constant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * 系统拦截器
 * @author 江雪林
 * @create 2021-03-18
 */
@Component
public class SysHandlerInterceptor implements HandlerInterceptor {
    private static final Logger log = LoggerFactory.getLogger(SysHandlerInterceptor.class);

    @Autowired
    private RedisUtils redisUtils;

    /**
     * 访问控制器前拦截请求，即：在 DispatchServlet 分发器，执行分发前执行
     *
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //拦截控制器
        String uri = request.getRequestURI();
        log.info("-> 请求 " + uri + " 访问 <-");
        //获取入参参数名+参数值，封装到 Map容器中
        Map<String, String> paramMap = ParamUtils.getKeyValuesMap(request);
        log.info("-> 请求参数：\n" + paramMap.toString());
        log.info("-> 请求调用处理器：" + handler.toString() + " <-");
        //判断访问的控制器是否请求登录，注册处理器
        //这里需要从请求头中获取 用户登录的标识 UID 、RID，同时去 redis 比较
//        String uid = request.getHeader(Constant.U_ID);
//        String rid = request.getHeader(Constant.R_ID);
//        System.out.println("请求头："+uid+","+rid);
//        if(uid!=null && rid!=null){
//            String token = redisUtils.get(uid);
//            System.out.println("token:"+token);
//            if((!"".equals(token)) && token.equals(rid)){
//                System.out.println("跳转");
//                return true;
//            }
//        }
        String uid = CookieUtils.getCookieValueByName(request,Constant.U_ID);
        String token = CookieUtils.getCookieValueByName(request,Constant.R_ID);
        if("".equals(uid) || "".equals(token) || uid==null || token == null){
            log.info("-> 请求 "+uri+"被拦截！！");
            response.sendRedirect("/login");
            return false;
        }
        if(!uid.equals(redisUtils.get(uid))){
            return false;
        }
        return true;
    }

    /**
     * 在 DispatchServlet 分发器，进行下一步视图页面渲染前执行
     *
     * @param request
     * @param response
     * @param handler
     * @param modelAndView
     * @throws Exception
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        log.info("-> 调用控制器：" + handler.toString() + " 进行数据处理 <-");
    }

    /**
     * 在进行视图页面渲染后，响应客户端之前执行
     *
     * @param request
     * @param response
     * @param handler
     * @param ex
     * @throws Exception
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        if (ex != null) {
            log.info("-> 调用控制器：" + handler.toString() + "发生异常，异常信息：\n" + ex.getMessage() + " <-");
        }
        log.info("-> 调用控制器：" + handler.toString() + "执行完毕 <-");
    }
}
